using ADODB;
using Microsoft.CommerceServer.Interop;
using Microsoft.CommerceServer.Interop.Configuration;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Diagnostics;
using Microsoft.CommerceServer.Runtime.Orders;
using MSXMLLib;
using System;
using System.Xml;

/// <summary>
/// Summary description for Class1.
/// </summary>
class SerializePurchaseOrder
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main(string[] args)
	{
		if (args.Length != 3)
		{
			Console.WriteLine("SerializePurchaseOrder.exe [SiteName] [UserId] [OrderGroupId]");
			return;
		}

		Console.WriteLine("Connecting to Commerce Server Site Configuration - {0}", args[0]);
		SiteConfigReadOnly SiteConfig = new SiteConfigReadOnlyClass();		
		SiteConfig.Initialize(args[0]);
		
		Console.WriteLine("Retrieving Transactions Resource Connection String");
		Recordset TransactionsResource = ((ADODB.Field)SiteConfig.Fields["Transactions"]).Value as ADODB.Recordset;
		string transactionConnectionString = ((ADODB.Field)TransactionsResource.Fields["connstr_db_Transactions"]).Value as string;
		
		Console.WriteLine("Retrieving Transaction Config Resource Connection String");
		ADODB.Recordset TransactionConfigResource = ((ADODB.Field)SiteConfig.Fields["Transaction Config"]).Value as ADODB.Recordset;
		string transactionConfigConnectionString = ((ADODB.Field)TransactionConfigResource.Fields["connstr_db_TransactionConfig"]).Value as string;

		Console.WriteLine("Initializing Order System");
		OrderContext Context = new OrderContext(transactionConnectionString, transactionConfigConnectionString, new ConsoleDebugContext(DebugMode.Debug));
		
		Guid userId = new Guid(args[1]);
		Guid orderId = new Guid(args[2]);

		Console.WriteLine("Retrieving Purchase Order from Database");
		PurchaseOrder purchaseOrder = Context.GetPurchaseOrder(userId, orderId);		

		Console.WriteLine("Serializing Purchase Order");
		DictionaryXMLTransformsFreeThreaded DictionaryXml = new DictionaryXMLTransformsFreeThreaded();
		IXMLDOMNode XmlInstance = DictionaryXml.GenerateSampleXMLInstanceFromDictionary(purchaseOrder.OrderForms[0], "OrderGroup", true);
	
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.LoadXml(XmlInstance.xml); 
		
		// Promote order group level items to the order form
		XmlNode root = xmlDocument.DocumentElement; 
		XmlAttribute attr = xmlDocument.CreateAttribute("order_status_code"); 
		attr.Value = purchaseOrder.StatusCode.ToString();  
		root.Attributes.Append(attr);

		attr = xmlDocument.CreateAttribute("order_number"); 
		attr.Value = purchaseOrder.TrackingNumber;
		root.Attributes.Append(attr); 

		attr = xmlDocument.CreateAttribute("site_name"); 
		attr.Value = args[0];
		root.Attributes.Append(attr); 
		
		string OrderFileName = String.Format("{0}-{1}.xml", args[0], purchaseOrder.TrackingNumber);
		Console.WriteLine("Writing to disk - {0}", OrderFileName);
		xmlDocument.Save(OrderFileName);
	}
}